package com.web.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.mall.entity.TbBrand;
import com.web.mall.entity.TbTypeTemplate;
import com.web.mall.req.TbTypeTemplateReq;
import com.web.mall.service.ITbBrandService;
import com.web.mall.service.ITbTypeTemplateService;
import com.web.mall.utils.ResultBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author s1x
 * @since 2022-12-08
 */
@RestController
@RequestMapping("/typeTemplate")
public class TbTypeTemplateController {

    @Autowired
    ITbTypeTemplateService tbTypeTemplateService;

    // 分页条件查询
    @GetMapping("/getByCondition")
    public ResultBean getByCondition(TbTypeTemplateReq typeTemplate) {
        System.out.println("\n分页条件查询typeTemplate:" + typeTemplate);
        //1. 分页拦截，重构分页sql语句
        Page pageInfo = new Page(typeTemplate.getPage(), typeTemplate.getSize());
        LambdaQueryWrapper<TbTypeTemplate> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(typeTemplate.getName()), TbTypeTemplate::getName, typeTemplate.getName());

        tbTypeTemplateService.page(pageInfo, wrapper);
        System.out.println("pageInfo:" + pageInfo);
        return ResultBean.ok(pageInfo);
    }

    // 根据id删除
    @GetMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Integer id) {
        return ResultBean.ok(tbTypeTemplateService.removeById(id));
    }


    // 根据id查找
    @GetMapping("/getById/{id}")
    public ResultBean getById(@PathVariable("id") Integer id) {
        return ResultBean.ok(tbTypeTemplateService.getById(id));
    }


    // 新增或更新
    @PostMapping("/addOrUpdate")
    public ResultBean addOrUpdate(@RequestBody TbTypeTemplate typeTemplate) {
        return ResultBean.ok(tbTypeTemplateService.saveOrUpdate(typeTemplate));
    }

}
